package com.hq.jpush;



import org.apache.log4j.Logger;

import com.hq.pojo.Conver;
import com.hq.service.impl.ConverImpl;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;



/**
 * 功能一: 根据别名推送一条话题 JPush.push(账户名,话题类)
 * 			
 * @author 不正
 *
 */
public class JPush {
	private static final Logger LOG = Logger.getLogger(JPush.class);

//	public static final String APP_KEY = "2b1221c7e0c891ad73846cda";
//	public static final String MASTER_SECRET = "179c856cc8196b15e9748344";
//	private static final String alias = "字母";
	public static final String APP_KEY = "4fe4afe4e45fb75fdcf4586b";
	public static final String MASTER_SECRET = "bfbd5134d622661fd7ebb438";

	private static final String alias = "sss";

	/**
	 * 根据别名创建PushPayload
	 * 
	 * @param alias
	 * @param conver
	 * @return
	 */
	public static PushPayload buildPushObject_android_alias(String alias, Conver conver) {
		if (alias == null || conver == null) {
			return null;
		}
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alias))
				.setNotification(Notification.newBuilder().setAlert(conver.getConver_content())
						.addPlatformNotification(AndroidNotification.newBuilder().setTitle(conver.getConver_title())
								.addExtra("circle_id", conver.getCircle_id())
								.addExtra("account_id", conver.getAccount_id())
								.addExtra("conver_image1", conver.getConver_image1())
								.addExtra("conver_image2", conver.getConver_image2())
								.addExtra("conver_image3", conver.getConver_image3())
								.addExtra("createAt", conver.getCreateAt().toString()).build())
						.build())
				.build();

	}

	/**
	 * 向别名推送话题
	 * 
	 * @param alias
	 */
	public static void push(String alias, Conver conver) {
		JPushClient jpushClient = new JPushClient(JPush.MASTER_SECRET, JPush.APP_KEY, null,
				ClientConfig.getInstance());
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_android_alias(alias, conver);
		try {
			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result - " + result);
			if (result != null && result.isResultOK()) {
				LOG.info("JPushDemo : 推送成功");
			} else {
				LOG.info("JPushDemo : 推送失败");
			}
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			LOG.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			LOG.error("Should review the error, and fix the request", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
		}
	}

	public static void main(String[] args) {
		JPush.push(JPush.alias, new ConverImpl().getOneConver());
	}

}
